package org.fig.ioc.bean.definition;


import org.fig.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: {@link org.springframework.beans.factory.config.BeanDefinition} 的构建示例
 * @Author: Flynn Vanderboom
 * @Data: 2021/6/14 2:55
 */

public class BeanDefinitionCreationDemo {
    public static void main(String[] args) {
        //1.通过BeanDefinitionBuilder构建
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        //通过属性设置
        beanDefinitionBuilder.addPropertyValue("id", 14)
                .addPropertyValue("name", "亮子");
        //获取BeanDefinition实例
        BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        //BeanDefinition 并非最终状态，可以自定义修改


        //2.通过派生类 AbstractBeanDefinition (或者其他派生类)构建
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        //设置Bean类型
        genericBeanDefinition.setBeanClass(User.class);
        //通过MutablePropertyValues设置属性
        MutablePropertyValues propertyValues = new MutablePropertyValues();
/*        propertyValues.addPropertyValue("name","公瑾");
        propertyValues.addPropertyValue("id",77);*/
        propertyValues.add("name", "公瑾")
                .add("id", 77);
        genericBeanDefinition.setPropertyValues(propertyValues);

    }
}
